package cn.spike.rockcc.mq.consumer;

import cn.spike.rockcc.domain.PayOrder;
import cn.spike.rockcc.dto.Order2Pay;
import cn.spike.rockcc.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author:spike
 * @create:2023-03-22 12:33
 */
@RocketMQMessageListener(
        consumerGroup="service-consumer-pay",
        topic = "order-topic",
        selectorExpression = "order-tags",
        messageModel = MessageModel.CLUSTERING
)
@Component
public class OrderConsumer implements RocketMQListener<String> {

    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(String message) {
        Order2Pay order2Pay = JSON.parseObject(message, Order2Pay.class);
        PayOrder payOrder = payOrderService.selectByOrderNo(order2Pay.getOrderNo());
        if(payOrder != null) return;
        payOrderService.createPayOrder(order2Pay);
    }
}
